let app = getApp();
var util = require("../../utils/util.js");
var siteinfo = require('../../siteinfo.js');
var starRun = 0;
var second = 0//计时，单位秒.
var startTime = ''//学习开始时间
var endTime = ''//学习结束时间
/* 计时 */
function count_down(that) {
  if (starRun == 0) {
    return;
  }
  setTimeout
  setTimeout(function() {
    if (second!=0){
      if (second % 600 == 0) {
        that.videoContext.pause()
        that.createCode()
        that.stopRun()
        that.setData({
          showModalMore: true
        });
      }
    }
//  console.log("second", second)
    second++
    count_down(that);
  }, 1000)
}
Page({

  data: {
    showModalMore: false,

  },

  onLoad: function(options) {
    this.setData({ courseId: options.courseId})
    startTime = util.formatTime(new Date())
   
    var videoContext = wx.createVideoContext('myvideo', this);
    this.videoContext = wx.createVideoContext('myvideo')　　 
    videoContext.requestFullScreen(); //执行全屏方法
    // this.createCode()

    this.setData({
      chapterId:options.chapterId,
      resourceId:options.resourceId
    })
  },
  onShow:function(){
    this.getChapterDetails(this.data.chapterId)
    this.getView()
  },

 // 页面销毁时执行
  onUnload: function () {
    var _this = this
    endTime = util.formatTime(new Date())
    app._getLogin(siteinfo.selIsMyCourse, {
      courseId: _this.data.courseId,
    }, function (result) {
      if (result.data.isMyCourse==0){
        app._getLogin(siteinfo.addLearns, {
          chapterId: _this.data.chapterId,
          times: second,
          startTime: startTime,
          endTime: endTime
        }, function (result) {
          second = 0
          starRun = 0;
          count_down(_this);
        })
      }
    })
    
  },


  //开始计时
  starRun: function() {
    if (starRun == 1) {
      return;
    }
    starRun = 1;
    count_down(this);
  },


  //暂停计时
  stopRun: function() {
    starRun = 0;
    count_down(this);
  },

  //获取验证码
  getVerifiCode: function(e) {
    var value = e.detail.value;
    if (this.data.code.toUpperCase() != value.toUpperCase()) {
      app.showError('验证码错误');
      return false;
    }
    this.videoContext.play()
    this.starRun()
    this.setData({
      showModalMore: false
    });
  },
  //换验证码图片
  changeCode() {
    this.createCode()
  },
  //验证码
  createCode() {
    var code;
    //首先默认code为空字符串
    code = '';
    //设置验证码长度
    var codeLength = 4;
    //设置随机字符
    var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    //循环codeLength
    for (var i = 0; i < codeLength; i++) {
      //设置随机数范围,这设置为0 ~ 36
      var index = Math.floor(Math.random() * 36);
      //字符串拼接 将每次随机的字符进行拼接
      code += random[index];
    }
    //将拼接好的字符串赋值给展示的code
    this.setData({
      code: code
    })
  },
  //获取详情数据
  getChapterDetails: function(id) {
    var _this = this
    app._getLogin(siteinfo.selChapterDetails, {
      id: _this.data.courseId
    }, function(result) {
      console.log(result)
      if (result.data.code == 40022 || result.data.code == 40025){
          wx.showModal({
            title: '友情提示',
            content: '您未购买此课程，请购买后重试！',
            showCancel:false,
            success:function(res){
              if (res.confirm){
                wx.navigateBack({})
              }
            }
          })
      }else{
        _this.starRun()
        _this.setData(result.data);
      }
    })
  },

  getView:function(resourceId){
    var _this = this
    wx.request({
      url: siteinfo.view,
      method: 'POST',
      header:{
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {resourceId:this.data.resourceId},
      success: function(res) {
      var traRes = res.data
      _this.setData({
        traRes:res.data
      })
      }
      
    })
  },
  //复制链接
  copy: function(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.url,
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: '链接已复制',
            })
          }
        })
      }
    })
  },

  // onShareAppMessage: function() {

  // }
})